/* ************************************
 * Heatmap Analysis
 * 
 * ************************************
 */


// number of stimuli to read


// constants
// ============
static final String	tree_directory		= "trees_full";
static final String	stimuli_list_file	= "experiment_stimuli_FULL.txt";

static final String	heatmap_directory	= "heatmaps";
static final String	aggregate_directory	= "aggregate";
static final String	subject_directory	= "subjects";

static final int		VIS_WIDTH		= 1280;
static final int 	VIS_HEIGHT		= 1024;
static final int		BUFFER_SIZE		= 1024 * 1024 * 5;
static final color	HILIGHT_COLOR		= #FF0000;

// Global variables
// =================
int maxChild, minChild, treeDepth, curDepth;
int lastTime = 0;

// current active offscreen buffer
PGraphics currentBuffer = null;

// stimuli
StimuliList stimuli = null;
ArrayList<String> subjects;

// fonts
PFont fontSmallQuestion = null;


float getTime()
{
	int curTime = millis();
	int delta = curTime - lastTime;
	lastTime = curTime;
	
	return (float) delta / 1000.0;
}

void setup()
{
	size(300, 300);
	smooth();
	
	// read stimuli list
	println("Reading stimuli list...");
	stimuli = new StimuliList( stimuli_list_file );
	stimuli.sort();
	
	// read list of subjects
	println("Reading available subject data..");
	subjects = new ArrayList<String>();
	File dir = new File( dataPath(subject_directory) );
	String[] children = dir.list();
	for (int i = 0; i < children.length; i++)
	{
		String dName = children[i];
		if (dName.length() > 7 && dName.substring(0, 7).compareToIgnoreCase("subject") == 0)
		{
			String subjectDir = subject_directory + "/" + dName; 
			println("\t- Adding " + subjectDir);
			subjects.add( subjectDir );
		}
	}
	
	// create other fonts
	fontSmallQuestion = createFont("HelveticaBold", 12.0);
}

void draw()
{
}

void keyPressed()
{
	if (key == 'p' || key == 'P')
	{
		aggregateTreemaps();
	}
}

PImage renderHeatmap( Stimulus stimulus, Heatmap theMap )
{
	// create offscreen buffer
	PGraphics buf = createGraphics(VIS_WIDTH, VIS_HEIGHT, JAVA2D);
	currentBuffer = buf;
	
	buf.beginDraw();
	{
		currentBuffer.smooth();
		stimulus.render();
		
		// render question on lower right corner
		currentBuffer.pushStyle();
		{
			currentBuffer.textFont( fontSmallQuestion );
			currentBuffer.textAlign( RIGHT, BOTTOM );
			currentBuffer.text( stimulus.question, VIS_WIDTH - 10, VIS_HEIGHT - 10 );
		}
		currentBuffer.popStyle();
		
		buf.image( theMap.getMap(), 0, 0 );
	}
	buf.endDraw();
	
	return buf.get(0, 0, buf.width, buf.height);
}

void aggregateTreemaps()
{
	
	println("\n \t*** Aggregating treemaps *** \n");
	
	
	// loop through stimuli
	for (int s = 0; s < stimuli.size(); s++)
	{
		int number = s;	
		Stimulus stimulus = stimuli.get(s);
		stimulus.state = PHASE_VIS;
		
		println("\t Stimulus " + number );
		
		// create an empty aggregate heatmap
		Heatmap aggregateMap = new Heatmap( number );
				
		// loop through subjects
		for (int subject = 0; subject < subjects.size(); subject++)
		{
			String subjectDir = subjects.get( subject );
			println("\t\t Subject: " + subjectDir);	
			
			// read heatmap from that subject
			Heatmap subjectMap = new Heatmap( number );
			subjectMap.read( dataPath(subjectDir + "/" + "heatmap_" + number + ".dat") );
			
			// render image and save it
			PImage theImage = renderHeatmap( stimulus, subjectMap );
			theImage.save( "data/" +  heatmap_directory + "/" + subjectDir + "/" + "heatmap_" + number + ".png" );
			
			// aggregate the image with the heatmap
			aggregateMap.addHeatmap( subjectMap );
		}
		
		// render the aggregate map
		aggregateMap.makePicture();
		PImage aggregateImage = renderHeatmap( stimulus, aggregateMap );
		aggregateImage.save( "data/" + heatmap_directory + "/" + aggregate_directory + "/" + "heatmap_" + number + ".png" );
	}
}

